<template>
  <div>
    <!-- 面包靴导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片面板 -->
    <el-card>
      <!-- 警告提示 -->
      <el-alert
        title="只允许为第三级分类设置参数"
        type="warning"
        :closable="false"
        show-icon
      ></el-alert>

      <!-- 级联选择框 -->
      <el-row class="topRow">
        <el-col>
          <span>请选择商品分类: </span>
          <el-cascader
            v-model="selectedCateKeys"
            :options="cateList"
            :props="{
              expandTrigger: 'hover',
              label: 'cat_name',
              value: 'cat_id',
            }"
            @change="handleChange"
          ></el-cascader>
        </el-col>
      </el-row>

      <!-- TABlan -->
      <el-tabs
        type="border-card"
        v-model="activeName"
        @tab-click="handleTabClick"
      >
        <!-- 添加参数的按钮 -->
        <el-button
          type="primary"
          :disabled="isBtnDisable"
          class="addBtn"
          size="mini"
          >添加参数</el-button
        >
        <!-- 动态参数 -->
        <el-tab-pane label="动态参数" name="many">动态参数</el-tab-pane>

        <!-- 静态属性 -->
        <el-tab-pane label="静态属性" name="only">静态属性</el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      cateList: [], // 用来保存商品分类列表的数组
      cateProps: { // 级联选择框的配置对象
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      selectedCateKeys: [], // 级联选择框双向数据绑定到的数组
      activeName: 'many' // 绑定要被选择的tab项

    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    // tab栏点击事件
    handleTabClick () {
      // console.log(this.activeName)
    },
    // 级联选择框变化事假
    async handleChange () {
      if (this.selectedCateKeys.length !== 3) {
        this.selectedCateKeys = []
        return false
      }
      console.log(this.selectedCateKeys)
      const { data: res } = await this.$http.get(`categories/${this.cateId}/attributes`, {
        params: {
          sel: this.activeName
        }
      })
      console.log(res.data)

      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
    },
    // 获取商品分类列表
    async  getCateList () {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.cateList = res.data
    }
  },
  computed: {
    // 计算是否选中三级权限,如果选中就启用添加参数按钮,反之no
    isBtnDisable () {
      if (this.selectedCateKeys.length !== 3) return true
      return false
    },
    // 计算出选中的是三级权限id
    cateId () {
      if (this.selectedCateKeys.length === 3) {
        return this.selectedCateKeys[2]
      }
      return null
    }
  }
}
</script>

<style lang="less" scoped>
.topRow {
  margin: 15px 0;
}

.addBtn {
  margin: 15px;
}
</style>
